﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class ContactLocationListControl : RSBaseUserControl
    {

        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void lnkViewLocations_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/RS/SBM/RSViewLocations.aspx");
        }
        protected void lnkViewMap_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int locationid = Convert.ToInt32(e.CommandArgument.ToString());

            ucModalViewMapControl.ShowMapDataByLocationID(companyid, locationid);
        }

        protected void gvLocationContactList_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;

                int contactid = Convert.ToInt32(hfSelectedContactId.Value);
                int locationid = Convert.ToInt32(gvLocationContactList.DataKeys[e.RowIndex].Value);

                RemoveLocationFromContact(companyid, locationid, contactid);
                ucModalActionMessageControl.ShowModal("Location removed from contact successfully!", Request.RawUrl, true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }


        private void RemoveLocationFromContact(int companyid, int locationid, int contactid)
        {
            hfSelectedContactId.Value = contactid.ToString();
            if (companyid > 0 && locationid > 0 && contactid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@locationid", SqlDbType.Int, 0, ParameterDirection.Input, locationid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@contactid", SqlDbType.Int, 0, ParameterDirection.Input, contactid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblContactLocation WHERE ContactId=@contactid AND  LocationId=@locationid AND CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            }
        }

        public void LoadLocationContactListData(int companyid, int contactid)
        {
            hfSelectedContactId.Value = contactid.ToString();
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select l.LocationId,l.LocationName,l.Street,l.City,l.County,l.State,l.Country,l.Zip,l.Active,l.Note,l.CompanyId from rs_tblLocation l inner join rs_tblContactLocation cl on l.locationid=cl.locationid where cl.contactid=@contactid and cl.companyid=@companyid order by l.city";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@contactid";
            param.Value = contactid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvLocationContactList.DataSource = ds;
            gvLocationContactList.DataBind();
            myConnection.Close();

        }


    }
}